import { ref, unref } from 'vue';
import filters from '@/utils/filter';
export const useCreateProductForm = (form, setForm) => {
    const createVersionRef = ref<any>();
    const searchVersionSelectCellRef = ref<any>();
    // 创建版本
    const handleCreateVersion = () => {
        unref(createVersionRef).open(+form.value.requireSystem);
    };
    // 创建版本成功回调
    const createVersionSuccess = () => {
        unref(searchVersionSelectCellRef).getOptions();
    };
    // 监听文件删除等操作
    const fileChange = (fileList: any[]) => {
        setForm({
            files: [...fileList]
        });
    };
    // 通用选择回调
    const handleSelectChange = (val, field: string) => {
        switch (field) {
            case 'taskRequire':
                setForm({
                    requireType: val ? (filters.valToNameFilter('source', val.source) === '缺陷' ? '4' : form.value.requireType) : null,
                    relateRequireId: val ? val.id : null,
                    relateRequireName: val ? val.requestName : ''
                });
                break;
            case 'version':
                setForm({
                    relateVersionName: val ? val.versionName : '',
                    relateVersionNumber: val ? val.versionNumber : '',
                    relateVersionId: val ? val.id : '',
                    expectTime: val ? val.expectTime : '',
                    onlineTime: val ? val.onlineTime : ''
                });
                break;
            case 'systemManager':
                setForm({
                    agentUser: val ? val.empName : '',
                    agentUserEmpId: val ? val.empId : ''
                });
                break;
            case 'requireSystem':
                setForm({
                    catalogCode: val ? val.catalogCode : '',
                    requireSystem: val ? val.systemId : '',
                    pmpLabels: [],
                    systemType: val ? val.systemType : '',
                    baseWorkHours: val ? val.baseWorkHours : '',
                    workHourCount: val ? val.workHourCount : '',
                    planWorkHours: isNaN(form.storyPoints * val.baseWorkHours * val.workHourCount)
                        ? 0
                        : form.storyPoints * val.baseWorkHours * val.workHourCount
                });
                if (val.systemType !== '3') {
                    setForm({
                        devType: val.systemType
                    });
                }
                break;
            case 'requireCatalogCode':
                setForm({
                    catalogCode: val ? val.catalogCode : '',
                    requireSystem: val ? val.id : ''
                });
                break;
            case 'label':
                setForm({
                    pmpLabels: [...val]
                });
                break;
        }
    };

    return {
        createVersionRef,
        searchVersionSelectCellRef,
        handleCreateVersion,
        createVersionSuccess,
        fileChange,
        handleSelectChange
    };
};
